package sk.tuke.constraintbuilder.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;

import sk.tuke.constraintbuilder.model.Constraint;
import sk.tuke.constraintbuilder.model.RootElement;
import sk.tuke.constraintbuilder.model.Ruleset;
import sk.tuke.constraintbuilder.model.Template;

public class XMLUtils {
	
	public static sk.tuke.constraintbuilder.model.Ruleset xmlToRuleset(File file){
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(sk.tuke.constraintbuilder.model.Ruleset.class);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

			try{
				Object model = unmarshaller.unmarshal(file);
				if(model instanceof sk.tuke.constraintbuilder.model.Ruleset){
					sk.tuke.constraintbuilder.model.Ruleset ruleset = (sk.tuke.constraintbuilder.model.Ruleset) model;
					return ruleset;
				}	
			}catch (UnmarshalException e) {
				e.printStackTrace();
			}
		} catch (JAXBException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return null;
	}

	public static sk.tuke.constraintbuilder.model.Constraint xmlToConstraint(File file) {
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(sk.tuke.constraintbuilder.model.Constraint.class);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

			try{
				Object model = unmarshaller.unmarshal(file);
				if(model instanceof sk.tuke.constraintbuilder.model.Constraint){
					sk.tuke.constraintbuilder.model.Constraint constraint = (sk.tuke.constraintbuilder.model.Constraint) model;
					return constraint;
				}	
			}catch (UnmarshalException e) {
				e.printStackTrace();
			}
		} catch (JAXBException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return null;
	}
	
	public static sk.tuke.constraintbuilder.model.Template xmlToTemplate(File file){
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(sk.tuke.constraintbuilder.model.Template.class);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

			try{
				Object model = unmarshaller.unmarshal(file);
				if(model instanceof sk.tuke.constraintbuilder.model.Template){
					sk.tuke.constraintbuilder.model.Template template = (sk.tuke.constraintbuilder.model.Template) model;
					return template;
				}	
			}catch (UnmarshalException e) {
				e.printStackTrace();
			}
		} catch (JAXBException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return null;
	}

}
